<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <div class="table-operator">
        <a-button v-action:add type="primary" icon="plus" @click="handleAdd">新建</a-button>
      </div>

      <s-table
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
      >

        <span slot="reason" slot-scope="text, record">
          <ellipsis :length="20" tooltip>{{ text }}</ellipsis>
        </span>
        <span slot="order_status" slot-scope="text, record">
          <a-badge status="default" v-if="record.order_status === '1'" text="保存"/>
          <a-badge status="processing" v-else-if="record.order_status === '2'" text="提交"/>
          <a-badge status="error" v-else text="撤销"/>
        </span>
        <span slot="approval_status" slot-scope="text, record">
          <a-badge status="success" v-if="record.approval_status === '1'" text="同意"/>
          <a-badge status="error" v-else-if="record.approval_status === '2'" text="拒绝"/>
          <a-badge status="default" v-else text="待处理"/>
        </span>
        <span slot="created_at" slot-scope="text">
          <span>{{ text | dayjs }}</span>
        </span>
        <span slot="updated_at" slot-scope="text">
          <span>{{ text | dayjs }}</span>
        </span>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record, 1)" v-action:query>查看</a>
          <template v-if="record.order_status === '1' || record.order_status === '3'">
            <a-divider type="vertical" v-action:edit/>
            <a @click="handleEdit(record, 2)"  v-action:edit>修改</a>
          </template>
          <template v-if="!record.approval_status">
            <a-divider type="vertical" v-action:revoke/>
            <a @click="handleEdit(record, 3)" v-action:revoke>撤销</a>
          </template>
          <template v-if="record.order_status === '2' && (!record.approval_status||record.approval_status=='4')">
            <a-divider type="vertical" v-action:agree/>
            <a @click="handleEdit(record, 4)" v-action:agree>同意</a>
            <a-divider type="vertical" v-action:refuse/>
            <a @click="handleEdit(record, 5)"  v-action:refuse>拒绝</a>
          </template>
        </span>
      </s-table>

      <create-form
        ref="createModal"
        :visible="visible"
        :loading="confirmLoading"
        :model="mdl"
        @cancel="handleCancel"
        @ok="handleOk"
      />

      <a-modal v-model="approval_status.visible" title="拒绝理由" @ok="handleApprovalOk" @cancel="handleApprovalCancel">
        <a-textarea placeholder="请输入拒绝理由" :rows="6"  v-model="approval_status.text"/>
      </a-modal>

      <a-modal v-model="agree_status.visible" title="同意类型" @ok="handleAgree">
          <a-checkbox-group  :options="types_em" v-model="agree_status.types"></a-checkbox-group>
      </a-modal>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import moment from 'moment'
import { STable, Ellipsis } from '@/components'
import { getList, add, edit, deletes, order_status, approval_status } from '@/api/examine'

import CreateForm from './modules/CreateForm'

const columns = [
  {
    title: '申请人',
    dataIndex: 'user_name'
  },
  {
    title: '描述',
    align: 'center',
    dataIndex: 'reason',
    scopedSlots: { customRender: 'reason' }
  },
  {
    title: '提交时间',
    align: 'center',
    dataIndex: 'created_at',
    scopedSlots: { customRender: 'created_at' }
  },
  {
    title: '申请状态',
    align: 'center',
    dataIndex: 'order_status',
    scopedSlots: { customRender: 'order_status' }
  },
  {
    title: '审核状态',
    align: 'center',
    dataIndex: 'approval_status',
    scopedSlots: { customRender: 'approval_status' }
  },
  {
    title: '操作',
    align: 'center',
    dataIndex: 'action',
    width: '150px',
    scopedSlots: { customRender: 'action' }
  }
]

export default {
  name: 'Dept',
  components: {
    STable,
    Ellipsis,
    CreateForm,
  },
  data () {
    this.columns = columns
    return {
      // create model
      visible: false,
       types_em:[

        {label:'养老保险',value:'1'},
        {label:'失业保险',value:'2'},
        {label:'医疗保险',value:'3'},
        {label:'工伤保险',value:'4'},
        {label:'生育保险',value:'5'},
      ],
      approval_status: {
        visible: false,
        text: undefined,
        id: undefined,
      },
        agree_status:{
          visible:false,
          types:[],
      },
      confirmLoading: false,
      refuseReason: undefined,
      mdl: null,
      // 查询参数
      queryParam: {},
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        return getList('no_insure_approval', requestParameters)
          .then(res => {
            return res.data
          })
      },
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  methods: {
    handleAdd () {
      this.mdl = {
        disabled: false
      }
      this.visible = true
    },
    handleEdit (record, index) {
      if (index === 1) {
        this.visible = true
        record.disabled = true;
        this.mdl = { ...record }
      } else if (index === 2) {
        this.visible = true
        record.disabled = false;
        this.mdl = { ...record }
      } else if (index === 3){
        this.confirmLoading = true
        order_status('no_insure_approval', record.id, '3').then(ret => {
          this.visible = false
          this.confirmLoading = false
          // 刷新表格
          this.$refs.table.refresh()
          this.$message.info('撤销成功')
        })
      } else {
        if (index == 4) {
          var types = [];
          if(record.approval_status=="4")
          {
              types = record.pass.split(',')
          }
          else
          {
            types =record.types.split(',')
          }
        this.agree_status = {
              visible:true,
              types:types,
              id:record.id
          }

        } else {
          this.approval_status = {
            visible: true,
            text: undefined,
            id: record.id,
          }
        }
      }
    },
        handleAgree(){
      if(this.agree_status.types.length==0)
      {
        this.$message.error('未选择类型')
        return;
      }

      this.confirmLoading = true
          approval_status('no_insure_approval', this.agree_status.id, {
            approval_status: '1',
            refuse_reason: '',
            pass:this.agree_status.types.join(",")
          }).then(ret => {
            this.visible = false
            this.confirmLoading = false
              this.agree_status = {
              visible:false,
              types:[],
          }
            // 刷新表格
            this.$refs.table.refresh()
            this.$message.info('已同意')
          })
    },
    handleApprovalOk() {
      if (!this.approval_status.text) {
        this.$notification.error({
          message: '拒绝理由不能为空',
        });
        return false;
      }
      this.confirmLoading = true
      approval_status('no_insure_approval', this.approval_status.id, {
        approval_status: '2',
        refuse_reason: this.approval_status.text,
      }).then(ret => {
        this.confirmLoading = false
        this.handleApprovalCancel();
        // 刷新表格
        this.$refs.table.refresh()
        this.$message.info('已拒绝')
      })
    },
    handleApprovalCancel() {
      this.approval_status = {
        visible: false,
        text: undefined,
        id: undefined,
      }
    },
    handleOk (values) {
      const form = this.$refs.createModal
      this.confirmLoading = true
      if (values.id) {
        edit('no_insure_approval', values, values.id).then(res => {
          this.visible = false
          this.confirmLoading = false
          // 重置表单数据
          form.resetFields()
          if (res.success) {
            // 刷新表格
            this.$refs.table.refresh()
            this.$message.info('修改成功')
          } else {
            this.$message.info(res.msg)
          }
        })
      } else {
        add('no_insure_approval', values).then(res => {
          this.visible = false
          this.confirmLoading = false
          // 重置表单数据
          form.resetFields()
         if (res.success) {
           // 刷新表格
           this.$refs.table.refresh()
           this.$message.info('新增成功')
         } else {
           this.$message.info(res.msg)
         }
        })
      }
    },
    handleCancel () {
      this.visible = false
      const form = this.$refs.createModal
      form.resetFields() // 清理表单数据（可不做）
    },
    resetSearchForm () {
      this.queryParam = {
        date: moment(new Date())
      }
    }
  }
}
</script>
